package p.ithorns.framework.xxljob;

import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.List;

/**
 * XxlJobAdminBizClient
 *
 * 扩展AdminBiz，使其可以自定义添加其他Header属性
 * 如Authorization
 *
 * job-admin的api前缀是“job-api”
 *
 * @author Ht.L
 * @date 2024-02-29 18:08
 * @since 1.0.0
 */
public class XxlJobAdminBizClient implements AdminBiz {

    private final static Logger log = LoggerFactory.getLogger(XxlJobAdminBizClient.class);

    /**
     * 认证
     */
    public static final String AUTHENTICATION = "Authorization";

    /**
     * 应用ID
     */
    public static final String X_APP_ID = "X-App-Id";

    private final AppContext context;

    private final XxlJobConf conf;

    private String addresses;

    public XxlJobAdminBizClient(AppContext context, XxlJobConf conf) {
        this.context = context;
        this.conf = conf;

        String addresses = conf.getAdmin().getAddresses();
        if (!addresses.endsWith("/")) {
            this.addresses = addresses + "/";
        }
    }

    @Override
    public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
        // return XxlJobRemotingUtil.postBody(addressUrl + "api/callback", accessToken, timeout, callbackParamList, String.class);
        return doRequest("job-api/callback", callbackParamList);
    }

    @Override
    public ReturnT<String> registry(RegistryParam registryParam) {
        // return XxlJobRemotingUtil.postBody(addresses + "api/registry", conf.getAccessToken(), 3, registryParam, String.class);
        return doRequest("job-api/registry", registryParam);
    }

    @Override
    public ReturnT<String> registryRemove(RegistryParam registryParam) {
        // return XxlJobRemotingUtil.postBody(addressUrl + "api/registryRemove", accessToken, timeout, registryParam, String.class);
        return doRequest("job-api/registryRemove", registryParam);
    }

    /**
     * 执行请求
     */
    private ReturnT<String> doRequest(String api, Object param) {
//        String url = addresses + api;
//        HttpRequest post = HttpRequest.post(url);
//        if (conf.isNeededApiToken()) {
//            String appToken = context.getAppToken();
//            post.header(AUTHENTICATION, appToken);
//            post.header(X_APP_ID, String.valueOf(context.getId()));
//            post.header(XxlJobRemotingUtil.XXL_JOB_ACCESS_TOKEN, conf.getAccessToken());
//
//        }
//
////        if (conf.getAdmin().isNeededCookie()) {
////            String cookie = getCookie();
////            post.cookie(cookie);
////        }
//
//        String jsonString = JSON.toJSONString(param);
//        HttpRequest request = post.body(jsonString);
//        try (HttpResponse response = request.execute()) {
//            log.info("xxl-job[{}] -> 请求参数: {}", url, jsonString);
//            if (!response.isOk()) {
//                String msg = "xxl-job remoting fail, StatusCode(" + response.getStatus() + ") invalid. for url : " + url;
//                return new ReturnT<>(ReturnT.FAIL_CODE, msg);
//            }
//            String body = response.body();
//            return JSON.parseObject(body, ReturnT.class);
//        } catch (Exception e) {
//            String errMsg = "xxl-job remoting error(" + e.getMessage() + "), for url : " + url;
//            return new ReturnT<String>(ReturnT.FAIL_CODE, errMsg);
//        }

        return new ReturnT<>();
    }

}